package common

import (
	"crypto/md5"
	"encoding/hex"
	"github.com/gin-gonic/gin"
	"go_batch_create_orders/utils"
	"strconv"
)

func Md5(str string) string {
	m5 := md5.New()
	_, err := m5.Write([]byte(str))
	if err != nil {
		panic(err)
	}
	md5String := hex.EncodeToString(m5.Sum(nil))
	return md5String
}

func JsonError(err string, ctx *gin.Context) {
	var data = utils.JsonInit()
	data.SetMessage(err)
	data.SetCode(502)
	ctx.JSON(200, data)
	return
}

func JsonSuccess(data interface{}, message string, ctx *gin.Context) {
	var json = utils.JsonInit()
	json.SetMessage(message)
	json.SetCode(200)
	json.SetData(data)
	ctx.JSON(200, json)
	return
}

//string 转化为 int
func StringToInt(string string) (res int, err error) {
	res, err = strconv.Atoi(string)
	return
}

//string 转化为 int64
func StringToInt64(string string) (res int64, err error) {
	res, err = strconv.ParseInt(string, 10, 64)
	return
}

//int 转化为 string
func IntToString(int int) (res string) {
	res = strconv.Itoa(int)
	return
}

//int64 转化为 string
func Int64ToString(int int64) (res string) {
	res = strconv.FormatInt(int, 10)
	return
}
